package com.yxlgateway.gateway.controller;

import com.yxlcommon.common.mapper.WorkCommentMapper;
import com.yxlcommon.common.mapper.WorksInfoMapper;
import com.yxlcommon.common.pojo.Likes;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlcommon.common.pojo.WorkComment;
import com.yxlcommon.common.pojo.WorksInfo;
import com.yxlgateway.service.LikesService;
import com.yxlgateway.service.impl.RedisServiceImpl;
import com.yxlcommon.common.vo.LikesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: yxl
 * @since: 2022-04-17
 **/
@Slf4j
@RestController
@RequestMapping("/likes")
public class LikesController {

    @Autowired
    private RedisServiceImpl redisService;
@Autowired
private WorksInfoMapper worksInfoMapper;
@Autowired
private WorkCommentMapper workCommentMapper;
    @Autowired
    private LikesService likesService;

    @Transactional
    @RequestMapping("/like")
    public RespBean saveLikeToRedis(@RequestBody Likes likes){
        if (likes.getType() == 1){
            WorksInfo worksInfo = new WorksInfo();
            worksInfo.setWorkId(likes.getObjId());
            worksInfo.setWorkLikesNums(worksInfoMapper.selectCountWorkLikes(likes.getObjId()) + 1);
            worksInfoMapper.updateById(worksInfo);
        } else {
            WorkComment workComment = new WorkComment();
            workComment.setCommentId(likes.getObjId());
            workComment.setContentPraise(workCommentMapper.selectCountPraise(likes.getObjId())+1);
            workCommentMapper.updateById(workComment);
        }
        return RespBean.operationSuccess("点赞成功", 200, likesService.save(likes));
    }

    @Transactional
    @RequestMapping("/unlike")
    public RespBean decrementLikeCount(@RequestBody Likes likes){
        if (likes.getType() == 1){
            WorksInfo worksInfo = new WorksInfo();
            worksInfo.setWorkId(likes.getObjId());
            worksInfo.setWorkLikesNums(worksInfoMapper.selectCountWorkLikes(likes.getObjId()) - 1);
            worksInfoMapper.updateById(worksInfo);
        } else {
            WorkComment workComment = new WorkComment();
            workComment.setCommentId(likes.getObjId());
            workComment.setContentPraise(workCommentMapper.selectCountPraise(likes.getObjId())-1);
            workCommentMapper.updateById(workComment);
        }
        return RespBean.operationSuccess("取消点赞成功",200,likesService.unSave(likes));
    }

    @RequestMapping("/restoreRedis")
    public RespBean restoreRedisCountInfo(){
        redisService.saveInfoFromDb2Re((byte) 0);
        redisService.saveInfoFromDb2Re((byte) 1);
        return RespBean.operationSuccess("恢复成功",200,1);
    }

    @PostMapping("/selectLike")
    public RespBean<Map<String, Object>> selectLike(@RequestBody Likes likes){
        return likesService.selectLike(likes);
    }

    @PostMapping("/selectWorksByLike")
    public RespBean<List<LikesVo>> selectWorksByLike(@RequestBody Likes likes){
        return likesService.selectWorksByLike(likes);
    }
}
